import meetingRoom from '../../config/api/meetingRoom' // 引入api接口管理文件
const dayjs = require("./dayjs");
const app = getApp()
var WxNotificationCenter = require("../../config/WxNotificationCenter");
import {
  baseUrl
} from '../../config/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showall: false,
    item: {},
    ferenceitem: {},
    name: null, //主题
    department: null, //部门
    phoneNumber: null, //手机号
    participants: null, // 人员名单
    leaderList: null, //领导名单
    meetingType: 1,
    meetingTypes: [{
        name: '0',
        value: '集团'
      },
      {
        name: '1',
        value: '分公司',
        checked: 'true'
      }
    ],

    ismonogram: 0, //是否有(不上传)
    monogramArr: ["无", "有"],
    monogram: null, //会标详情
    monogramImgname: null, //会标图片名称 (本地显示)
    monogramImg: null, // 会标图片
    peopleNub: null, //参会人数
    host: null, // 主持人

    isVideo: 0, //是否使用视频系统
    videoType: 0, //视频类型


    isMike: 0, // 是否使用麦克

    mikeType: 0, //麦克类型

    mikeTypeArr: ["有线", "无线"],
    mikeNum: null, // 麦克数量

    isComputer: 0,
    isComputerArr: ["自带", "提供"],

    isProjector: 0, // 是否使用投影仪    

    isPhotograph: 0, // 是否照相

    isKeepSecret: 0, //保密会议

    isSpokesman: 0, // 是否有发言人

    spokesman: null, // 发言人

    placeType: 0, //会议桌型
    placeTypeArr: ["课桌型", "U型", "回字型", "对面摆"],

    isSupplies: 0, //会议用品

    supplies: "", //会议用品
    suppliesArr: [{
        name: '桌牌',
        checked: false

      },
      {
        name: '矿泉水',
        checked: false
      },
      {
        name: '白瓷杯',
        checked: false

      },
      {
        name: '纸巾',
        checked: false

      },
      {
        name: '水壶',
        checked: false

      },
    ],

    isConnect: 0, // 是否连接电厂
    connect: '',
    connectArr: [{
        name: '大连',
        checked: false

      },
      {
        name: '丹东',
        checked: false
      },
      {
        name: '营口',
        checked: false

      },
      {
        name: '营热',
        checked: false

      }, {
        name: '清能',
        checked: false
      },
      {
        name: '仙人岛',
        checked: false
      },
      {
        name: '大连二热',
        checked: false
      },
      {
        name: '能销',
        checked: false

      },
      {
        name: '沈北',
        checked: false
      },

    ],
    conferenceAttendant: null, //会议服务员
    connectionTime: null, //集团要求链接时间
    contactPerson: null, //集团会议联络人信息
    remark: null, //特殊说明事项
    videoTypeArr: ["内网(集团)", "外网"],
    shifouArr: ["否", "是"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let theitem = JSON.parse(options['item']);
    console.log(theitem);

    let datestr = dayjs(theitem.date).format("YY/MM/DD");
    this.setData({
      item: theitem,
      ferenceitem: theitem.ferenceitem,
      datestr: datestr
    })
  },

  clicktijiao() {
    //必填字段判空
    let pkmap = {
      "name": "请输入主题",
      "department": "请输入部门",
      "phoneNumber": "请输入手机号码",
      "participants": "请输入人员名单",
      "leaderList": "请输入领导名单",
    }
    let allkey = Object.keys(pkmap);
    for (let index = 0; index < allkey.length; index++) {
      const element = allkey[index];
      console.log(this.data[element]);
      if (!this.data[element] || this.data[element].length < 1) {
        wx.showToast({
          title: pkmap[element],
          icon: "none",
        })
        return;
      }
    }
    //分割字符串
    let times = this.data.item.time.split('-');
    wx.showLoading({
      title: '预约中...',
      mask: true,
    })
    let obj = {
      "startTime": this.data.item.date + " " + times[0] + ":00",
      "roomId": this.data.ferenceitem.id,
      "openId": app.globalData.openid,
      "endTime": this.data.item.date + " " + times[1] + ":00",
      "type": 1
    };
    //外部字段
    let outkeys = ['name', 'department', 'phoneNumber', 'remark', 'peopleNub', 'participants', 'leaderList'];
    //内部字段
    let detailkeys = ['conferenceAttendant', 'connect', 'connectionTime', 'contactPerson', 'host', 'id', 'isComputer', 'isConnect', 'isKeepSecret', 'isMike', 'isNotice', 'isPhotograph', 'isProjector', 'isSpokesman', 'isSupplies', 'isVideo', 'meetingType', 'mikeNum', 'mikeType', 'monogram', 'monogramImg', 'placeType', 'spokesman', 'supplies', 'videoType'];

    for (let index = 0; index < outkeys.length; index++) {
      const key = outkeys[index];
      let data = this.data[key];
      if (data && data.length > 0) {
        obj[key] = data
      }
    }
    let details = {};

    for (let index = 0; index < detailkeys.length; index++) {
      const key = detailkeys[index];
      let data = this.data[key];
      if (data && data.length > 0) {
        details[key] = data
      }
    }
    if (this.data.showall) {
      obj["details"] = details
    }
    console.log(obj);

    meetingRoom.save(obj, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.status == 200) {
        wx.showModal({
          title: '预约成功',
          showCancel: false,
          success: (result) => {},
          fail: (res) => {},
          complete: (res) => {
            wx.navigateBack({
              delta: 1,
            })
          },
        })
        WxNotificationCenter.postNotificationName("getRecord");

      } else if (res.data.status == 1) {
        wx.showToast({
          title: "预定会议时间冲突",
          icon: "none"
        })
      } else if (res.data.status == 2) {
        wx.showToast({
          title: "会议室不存在",
          icon: "none"
        })
      } else if (res.data.status == 3) {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },

  //处理文本双向绑定问题
  onChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  //处理是否选项
  shifouChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value,
    })
  },

  //处理带数组类型的绑定
  moretypechange(e) {
    let arr = this.data[e.currentTarget.dataset.arrdata];
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  //处理单选
  // 会议类型
  meetingTypeChange: function (e) {
    this.setData({
      meetingType: e.detail.value
    })
  },

  //会议用品选择
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    let str = '';
    e.detail.value.map(res => {
      if (str.length > 0) {
        str = str + ',';
      }
      str = str + res;
      return res;
    })
    this.setData({
      supplies: str,
    })
  },
  //处理链接电厂
  selectconnect(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    let str = '';
    e.detail.value.map(res => {
      if (str.length > 0) {
        str = str + ',';
      }
      str = str + res;
      return res;
    })
    this.setData({
      connect: str,
    })
  },

  clickshowall() {
    let show = !this.data.showall;
    console.log(show);
    this.setData({
      showall: show
    })
  },
  // 上传文件
  clickupfile() {
    let vm = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        // tempFilePath可以作为 img 标签的 src 属性显示图片
        const tempFilePaths = res.tempFiles
        let url = baseUrl + 'aliyunFile/uploadFile';
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          success(res) {

            const data = res.data
            let urlpath = JSON.parse(data)["data"];
            let name = tempFilePaths[0].name;
            vm.setData({
              monogramImgname: name,
              monogramImg: urlpath
            })
          },
          fail(error) {
            console.log(error);
          },
          complete() {
            console.log("complete");
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})